﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration.Assemblies;
using System.Reflection;
using System.Globalization;
using System.ComponentModel;

namespace Kokomo.Mirror.CliMetadata.Tables
{
	public class AssemblyRow : IMetadataRow
	{
		public AssemblyHashAlgorithm HashAlgId { get; set; }
		public ushort MajorVersion { get; set; }
		public ushort MinorVersion { get; set; }
		public ushort BuildNumber { get; set; }
		public ushort RevisionNumber { get; set; }
		public AssemblyFlags Flags { get; set; }
		public BlobRef PublicKey { get; set; }
		public StringRef Name { get; set; }
		public StringRef Culture { get; set; }

		public AssemblyName ToAssemblyName(MetadataContainer container)
		{
			string cultureCode = container.GetString(this.Culture);
			CultureInfo culture = (cultureCode == null) ? CultureInfo.InvariantCulture : CultureInfo.GetCultureInfo(cultureCode);
			AssemblyName thisName = new AssemblyName()
			{
				CultureInfo = culture,
				HashAlgorithm = this.HashAlgId,
				Name = container.GetString(this.Name),
				Version = new System.Version(this.MajorVersion, this.MinorVersion, this.BuildNumber, this.RevisionNumber)
			};
			thisName.SetPublicKey(container.GetBlob(this.PublicKey));
			return thisName;
		}

		public override string ToString(MetadataContainer container)
		{
			return this.ToAssemblyName(container).ToString();
		}
	}
}
